package com.fling.abcde.ui.citypicker.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fling.abcde.BaseApplication;
import com.fling.abcde.R;
import com.fling.abcde.ui.citypicker.CityPickerActivity;
import com.fling.abcde.ui.citypicker.model.City;
import com.fling.abcde.ui.citypicker.utils.PinyinUtils;
import com.lib.common.view.NoScrollGridView;

import java.util.ArrayList;
import java.util.HashMap;

import static com.fling.abcde.ui.citypicker.CityPickerActivity.LOCATING;

public class CityListAdapter extends BaseAdapter {

    private static final int VIEW_TYPE_COUNT = 3;

    private LayoutInflater mInflater;
    private ArrayList<City> mCities;
    private HashMap<String, Integer> letterIndexes;
    private OnCityClickListener onCityClickListener;
    private int locateState = LOCATING;
    private String locatedCity;
    private TextView mState;

    public CityListAdapter(ArrayList<City> cities) {

        mCities = cities;
        mInflater = LayoutInflater.from(BaseApplication.sContext);
        if (cities == null) {
            cities = new ArrayList<>();
        }
        cities.add(0, new City("定位", "0"));
        cities.add(1, new City("热门", "1"));
        int size = cities.size();
        letterIndexes = new HashMap<>();
        // String[] sections = new String[size];
        for (int index = 0; index < size; index++) {
            //当前城市拼音首字母
            String currentLetter = PinyinUtils.getFirstLetter(cities.get(index).getPinyin());
            //上个首字母，如果不存在设为""
            String previousLetter = index >= 1 ? PinyinUtils.getFirstLetter(cities.get(index - 1).getPinyin()) : "";
            if (!TextUtils.equals(currentLetter, previousLetter)) {
                letterIndexes.put(currentLetter, index);
                // sections[index] = currentLetter;
            }
        }
    }

    /**
     * 更新定位状态
     *
     * @param state 状态
     * @param city  城市
     */
    public void updateLocateState(int state, String city) {

        this.locateState = state;
        this.locatedCity = city;
        notifyDataSetChanged();
    }

    /**
     * 获取字母索引的位置
     *
     * @param letter 字母
     */
    public int getLetterPosition(String letter) {

        Integer integer = letterIndexes.get(letter);
        return integer == null ? -1 : integer;
    }

    @Override
    public int getViewTypeCount() {

        return VIEW_TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {

        return position < VIEW_TYPE_COUNT - 1 ? position : VIEW_TYPE_COUNT - 1;
    }

    @Override
    public int getCount() {

        return mCities == null ? 0 : mCities.size();
    }

    @Override
    public City getItem(int position) {

        return mCities == null ? null : mCities.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        CityViewHolder holder;
        int viewType = getItemViewType(position);
        switch (viewType) {
            case 0:     //定位
                convertView = mInflater.inflate(R.layout.cp_view_locate_city, parent, false);
                ViewGroup container = (ViewGroup) convertView.findViewById(R.id.layout_locate);
                mState = (TextView) convertView.findViewById(R.id.tv_located_city);
                switch (locateState) {
                    case LOCATING:
                        mState.setText(BaseApplication.sContext.getString(R.string.cp_locating));
                        break;
                    case CityPickerActivity.FAILED:
                        mState.setText(R.string.cp_located_failed);
                        break;
                    case CityPickerActivity.SUCCESS:
                        mState.setText(locatedCity);
                        break;
                }
                container.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        if (locateState == CityPickerActivity.FAILED) {
                            //重新定位
                            if (onCityClickListener != null) {
                                onCityClickListener.onLocateClick();
                            }
                        } else if (locateState == CityPickerActivity.SUCCESS) {
                            //返回定位城市
                            if (onCityClickListener != null) {
                                onCityClickListener.onCityClick(locatedCity);
                            }
                        }
                    }
                });
                break;
            case 1:     //热门
                convertView = mInflater.inflate(R.layout.cp_view_hot_city, parent, false);
                NoScrollGridView gridView =  convertView.findViewById(R.id.gridview_hot_city);
                final HotCityGridAdapter hotCityGridAdapter = new HotCityGridAdapter();
                gridView.setAdapter(hotCityGridAdapter);
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        if (onCityClickListener != null) {
                            onCityClickListener.onCityClick(hotCityGridAdapter.getItem(position));
                        }
                    }
                });
                break;
            case 2:     //所有
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.cp_item_city_listview, parent, false);
                    holder = new CityViewHolder();
                    holder.letter = (TextView) convertView.findViewById(R.id.tv_item_city_listview_letter);
                    holder.name = (TextView) convertView.findViewById(R.id.tv_item_city_listview_name);
                    convertView.setTag(holder);
                } else {
                    holder = (CityViewHolder) convertView.getTag();
                }
                if (position >= 1) {
                    final String city = mCities.get(position).getName();
                    holder.name.setText(city);
                    String currentLetter = PinyinUtils.getFirstLetter(mCities.get(position).getPinyin());
                    String previousLetter = position >= 1 ? PinyinUtils.getFirstLetter(mCities.get(position - 1).getPinyin())
                            : "";
                    if (!TextUtils.equals(currentLetter, previousLetter)) {
                        holder.letter.setVisibility(View.VISIBLE);
                        holder.letter.setText(currentLetter);
                    } else {
                        holder.letter.setVisibility(View.GONE);
                    }
                    holder.name.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            if (onCityClickListener != null) {
                                onCityClickListener.onCityClick(city);
                            }
                        }
                    });
                }
                break;
        }
        return convertView;
    }

    public void setOnCityClickListener(OnCityClickListener listener) {

        this.onCityClickListener = listener;
    }

    public String getLocated() {

        return mState.getText().toString();
    }

    public interface OnCityClickListener {

        void onCityClick(String name);

        void onLocateClick();
    }

    private static class CityViewHolder {

        TextView letter;
        TextView name;
    }
}